/**
 * 
 */
package edu.bu.grocery.backingbean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;

import edu.bu.grocery.session.UserSession;
import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryUser;
import edu.bu.grocerylist.service.IGroceryListService;
import edu.bu.grocerylist.service.IGroceryUserService;

/**
 * This backing bean represents the GroceryLists page, where the user can see their
 * available GroceryList objects that they can perform as well as how to interact with them
 * @author donlanp
 *
 */
public class GroceryListsBackingBean  implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2408207935389475608L;

	private UserSession session;
	
	private IGroceryListService listService;
	
	private IGroceryUserService userService;
	
	public List<GroceryList> getUsersLists(){
		List<GroceryList> lists = listService.getUsersGroceryList(session.getCurrentUser());
		return lists;
	}

	@PostConstruct
	public void init(){
		System.out.println("Running...");
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String listToDelete = facesContext.getExternalContext().getRequestParameterMap().get("groceryList");
		String delete = facesContext.getExternalContext().getRequestParameterMap().get("delete");
		System.out.println(facesContext.getExternalContext().getRequestParameterMap());
		System.out.println(listToDelete);
		int listId = 0;
		if(listToDelete != null && delete != null){
			//If we're editing a current list, find the list that was passed in as a parameter
			listId = Integer.parseInt(listToDelete);
			GroceryList groceryList = listService.getGroceryListById(listId);
			listService.deleteGroceryList(groceryList);
	
		} 
		
	}
	
	/**
	 * Deletes a grocery list
	 * @return
	 */
	public boolean deleteList(GroceryList grocerylist) {
		boolean retval = listService.deleteGroceryList(grocerylist);
		return retval;
	}
	
	/**
	 * Used to determine if the GroceryList list is empty
	 * @return true if the list is empty, false otherwise
	 */
	public boolean getGroceryListEmpty() {
		return listService.getUsersGroceryList(session.getCurrentUser()).size() == 0;
	}
	
	/**
	 * Called when we want to add a new grocerylist, adds a new list to the session as a "temp" object
	 * @return JSF String for navigating to the grocerylist edit page.
	 */
	public String addGroceryList() {
		GroceryList groceryList = new GroceryList();
		session.setCurrentGroceryList(groceryList);
		return "grocerylist";
	}
	
	public String editGroceryList() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String groceryliststringid = facesContext.getExternalContext().getRequestParameterMap().get("groceryList");
		int grocerylistid = Integer.parseInt(groceryliststringid);
		GroceryList grocerylist = listService.getGroceryListById(grocerylistid);
		session.setCurrentGroceryList(grocerylist);
		return "grocerylist";
	}
	
	/**
	 * Used to template an existing grocerylist.  We obtain the information of the list we want to edit and 
	 * template it, storing the temporary object to the session then navigating to the new screen.
	 * @return The JSF String for the grocerylist page
	 */
	public String templateGroceryList() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String groceryliststringid = facesContext.getExternalContext().getRequestParameterMap().get("groceryList");
		int grocerylistid = Integer.parseInt(groceryliststringid);
		GroceryList grocerylisttemplate = listService.getGroceryListById(grocerylistid);
		GroceryList grocerylist = listService.templateGroceryList(grocerylisttemplate);
		session.setCurrentGroceryList(grocerylist);
		return "grocerylist";
	}
	
	/**
	 * Deletes the selected GroceryList (sends to the confirmation screen)
	 * @return
	 */
	public String deleteGroceryList(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String groceryliststringid = facesContext.getExternalContext().getRequestParameterMap().get("deleteGroceryList");
		int grocerylistid = Integer.parseInt(groceryliststringid);

		GroceryList listToDelete = listService.getGroceryListById(grocerylistid);
		session.setCurrentGroceryList(listToDelete);
		
		return "deletegrocerylist";
	}
	
	
	/**
	 * Deletes the currently logged in user.  Then logs them out.
	 * @return
	 */
	public String deleteUser(){
		GroceryUser user = session.getCurrentUser();
		System.out.println(user);
		userService.deleteUserFromSystem(user);
		
		//Nullify the session
		session.setCurrentUser(null);
		
		return "logout";
	}
	
	//Getters and Setters

	
	/**
	 * @return the session
	 */
	public UserSession getSession() {
		return session;
	}

	/**
	 * @param session the session to set
	 */
	public void setSession(UserSession session) {
		this.session = session;
	}

	/**
	 * @return the listService
	 */
	public IGroceryListService getListService() {
		return listService;
	}

	/**
	 * @param listService the listService to set
	 */
	public void setListService(IGroceryListService listService) {
		this.listService = listService;
	}

	/**
	 * @return the userService
	 */
	public IGroceryUserService getUserService() {
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	public void setUserService(IGroceryUserService userService) {
		this.userService = userService;
	}
}
